<?php

namespace app\admin\model;

use think\Model;

use think\Db;
use think\Request;
use think\Exception;//事务处理

class Area extends Model
{
    public function getarea($res)
    {
        $page = $res['page'];
        $limit = $res['pageSize'];
        //点击不同的标签获取不同信息
        if (isset($res['statusarea']) && $res['statusarea']) {
            $table = $res['statusarea'];
        } else {
            $table = 'country';
        }
        if ($table == 'country') {
            $data = Db::name($table)->page($page, $limit)->select();
            $count = Db::name($table)->count();
            $ids = array_column($data, 'id');
        } elseif ($table == 'provinces') {
            $data = Db::name($table)->page($page, $limit)->select();
            foreach ($data as $datakey => $dataval) {
                $country = getinfo($dataval['country_id'], 'country');
                $data[$datakey]['countryname'] = $country['name'];
                $data[$datakey]['telnum'] = $country['tel'];
            }
            $count = Db::name($table)->count();
            $ids = array_column($data, 'id');
        } elseif ($table == 'city') {
            $data = Db::name($table)->page($page, $limit)->select();
            foreach ($data as $datakey => $dataval) {
                $provinces = Db::name('provinces')->alias('a')->field('a.name as provincesname,b.name as countryname')->where('a.id', $dataval['province_id'])->join('nt_country b', 'a.country_id=b.id', 'left')->find();
                $data[$datakey]['provincesname'] = $provinces['provincesname'];
                $data[$datakey]['countryname'] = $provinces['countryname'];
            }
            $count = Db::name($table)->count();
            $ids = array_column($data, 'id');
        } elseif ($table == 'town') {
            $join = [
                ['nt_city b', 'a.city_id=b.id'],
                ['nt_provinces c', 'b.province_id=c.id'],
                ['nt_country d', 'c.country_id=d.id'],
            ];
            $field = 'a.*,b.name as cityname,c.name as provincesname,d.name as countryname';
            $data = Db::name($table)->alias('a')->field($field)->join($join)->page($page, $limit)->select();
            $count = Db::name($table)->alias('a')->join($join)->count();
            $ids = array_column($data, 'id');
        }
        return array('ids' => $ids, 'count' => $count, 'data' => $data);
    }

    //国家，省份，城市，城镇的添加的编辑
    public function addeditarea($data)
    {
        if ($data['act'] == 'countryadd' || $data['act'] == 'countryedit') {
            //国家的添加
            $ckdata = notEmpty(['name' => lang("country"), 'tel' => lang("phone_area_code"), 'short_name' => lang("abbreviation")]);
        } elseif ($data['act'] == 'provincesadd' || $data['act'] == 'provincesedit') {
            $ckdata = notEmpty(['country_id' => lang("country"), 'name' => lang("state"), 'zipcode' => lang("ZIP_CODE"), 'sn' => lang("abbreviation")]);
        } elseif ($data['act'] == 'cityadd' || $data['act'] == 'cityedit') {
            $ckdata = notEmpty(['country_id' => lang("country"), 'province_id' => lang("state"), 'name' => lang("city")]);
        } elseif ($data['act'] == 'townadd' || $data['act'] == 'townedit') {
            $ckdata = notEmpty(['country_id' => lang("country"), 'province_id' => lang("state"), 'city_id' => lang("city"), 'name' => lang("town")]);
        }
        if ($ckdata['status']==2) {
            return $ckdata;
        }
        $act = $data['act'];
        unset($data['act']);
        if ($act == 'countryadd') {
            //国家的添加
            unset($data['id']);
            $data['language_code'] = '';//语言代号
            $data['currncy'] = '';
            $counid = Db::name('country')->insert($data);
            if ($counid) {
                return array('msg' => $data['name'] . lang("added_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['name'] . lang("add_failed"), 'status' => 0);
            }
        } elseif ($act == 'countryedit') {
            $incoun = Db::name('country')->where('id', $data['id'])->update($data);
            if ($incoun !== false) {
                return array('msg' => $data['name'] . lang("edited_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['name'] . lang("edit_failed"), 'status' => 0);
            }
        } elseif ($act == 'provincesadd') {
            unset($data['id']);
            //省份添加
            $data['capital'] = '';//首都
            $proid = Db::name('provinces')->insert($data);
            if ($proid) {
                return array('msg' => $data['name'] . lang("added_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['name'] . lang("add_failed"), 'status' => 0);
            }
        } elseif ($act == 'provincesedit') {
            //省份的编辑
            $editpro = Db::name('provinces')->where('id', $data['id'])->update($data);
            if ($editpro !== false) {
                return array('msg' => $data['name'] . lang("edited_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['name'] . lang("edit_failed"), 'status' => 0);
            }
        } elseif ($act == 'cityadd') {
            unset($data['country_id']);
            //城市的添加
            $cityid = Db::name('city')->insert($data);
            if ($cityid) {
                return array('msg' => $data['name'] . lang("added_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['name'] . lang("add_failed"), 'status' => 0);
            }
        } elseif ($act == 'cityedit') {
            //城市的编辑
            unset($data['country_id']);
            $editcity = Db::name('city')->where('id', $data['id'])->update($data);
            if ($editcity !== false) {
                return array('msg' => $data['name'] . lang("edited_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['name'] . lang("edit_failed"), 'status' => 0);
            }
        } elseif ($act == 'townadd') {
            unset($data['id']);
            unset($data['country_id']);
            unset($data['province_id']);
            $townid = Db::name('town')->insert($data);
            if ($townid) {
                return array('msg' => $data['name'] . lang("added_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['name'] . lang("add_failed"), 'status' => 0);
            }
        } elseif ($act == 'townedit') {
            unset($data['country_id']);
            unset($data['province_id']);
            $edittownid = Db::name('town')->where('id', $data['id'])->update($data);
            if ($edittownid !== false) {
                return array('msg' => $data['name'] . lang("edited_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['name'] . lang("edit_failed"), 'status' => 0);
            }
        }
    }

    //国家的删除,省份的删除，城市的删除，城镇的删除
    public function counprocitown($data)
    {
        $act = $data['act'];
        $id = $data['id'];
        if ($act == 'countrydel') {
            $countryname = getinfo($id, 'country')['name'];
            //删除国家
            $pro = Db::name('provinces')->where('country_id', $id)->select();
            if ($pro) {
                return array('msg' => $countryname . lang("provinces_can't_be_deleted"), 'status' => 0);
            } else {
                $ret = Db::name('country')->where('id', $id)->delete();
                $success = $countryname . lang("deleted_successfully");
                $filed = $countryname . lang("failed_to_delete");
            }
        } elseif ($act == 'proesdel') {
            $provincesname = getinfo($id, 'provinces')['name'];
            //删除省份
            $city = Db::name('city')->where('province_id', $id)->select();
            if ($city) {
                return array('msg' => $provincesname . lang("cities_can't_be_deleted"), 'status' => 0);
            } else {
                $ret = Db::name('provinces')->where('id', $id)->delete();
                $success = $provincesname . lang("deleted_successfully");
                $filed = $provincesname . lang("failed_to_delete");
            }
        } elseif ($act == 'citydel') {
            $cityname = getinfo($id, 'city')['name'];
            //删除城市
            $town = Db::name('town')->where('city_id', $id)->select();
            if ($town) {
                return array('msg' => $cityname . lang("town_cant't_be_deleted"), 'status' => 0);
            } else {
                $ret = Db::name('city')->where('id', $id)->delete();
                $success = $cityname . lang("deleted_successfully");
                $filed = $cityname . lang("failed_to_delete");
            }
        } elseif ($act == 'towndel') {
            $townname = getinfo($id, 'town')['name'];
            //删除城镇
            $ret = Db::name('town')->where('id', $id)->delete();
            $success = $townname . lang("deleted_successfully");
            $filed = $townname . lang("failed_to_delete");
        }
        if ($ret) {
            return array('status' => 1, 'msg' => $success);
        } else {
            return array('status' => 0, 'msg' => $filed);
        }
    }
}